package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.resultCode.Result;
import com.woniuxy.resultCode.ResultCode;
import com.woniuxy.system.entity.PerformanceDepartment;
import com.woniuxy.system.service.PerformanceDepartmentService;
import com.woniuxy.system.view.PerformanceDepartmentView;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/system/performanceDepartment")
@Slf4j
@RequiresAuthentication
public class PerformanceDepartmentController {
    @Autowired
    PerformanceDepartmentService performanceDepartmentService;


    @PostMapping("/findUserByDId")
    public Result findUserByDId(Long id){
       Integer num =  performanceDepartmentService.findUserByDId(id);
       if (num!=null){
           return Result.ok().data("num",num);
       }else {
           return Result.error();
       }
    }

    @ApiOperation("获取所有的部门名称")
    @PostMapping("/list")
    public Result list(@RequestBody(required = false) PerformanceDepartmentView performanceDepartmentView ){
        log.info("获取参数:{}",performanceDepartmentView);
        QueryWrapper<PerformanceDepartment> wrapper=getWrapper(performanceDepartmentView);
        Page<PerformanceDepartment> page = new Page<>(performanceDepartmentView.getCurrentPage(),performanceDepartmentView.getPageSize());
        Page<PerformanceDepartment> result = performanceDepartmentService.getAllPerformanceDepartment(page,wrapper);
        List<PerformanceDepartment> records = result.getRecords();
        long total = result.getTotal();
        long totalPages = result.getPages();
        log.info("返回数据:total{},totalPages{}",total,totalPages);
        return Result.ok().data("msg",records).data("total",total).data("totalPages",totalPages);
    }


    @ApiOperation("新增部门")
    @PostMapping("/add")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result add(@RequestBody PerformanceDepartment performanceDepartment){
        boolean flage=performanceDepartmentService.addPerformanceDepartment(performanceDepartment);
        if (flage){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation("修改部门")
     @PostMapping("/update")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public  Result update(@RequestBody PerformanceDepartment performanceDepartment){
        boolean flag=performanceDepartmentService.updateById(performanceDepartment);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }

    }

    @ApiOperation("通过id删除部门")
    @DeleteMapping("/delete/{id}")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result daleteById(@PathVariable Integer id){
        boolean flag=performanceDepartmentService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error().code(ResultCode.DELETE_ERROR.getCode()).message(ResultCode.DELETE_ERROR.getMessage());
        }
    }

    //封装queryWrapper(条件构造器)
    private QueryWrapper<PerformanceDepartment> getWrapper(PerformanceDepartmentView  performanceDepartmentView) {
        QueryWrapper<PerformanceDepartment> queryWrapper = new QueryWrapper<>();
        if (performanceDepartmentView != null) {
            if (StringUtils.isNotBlank(performanceDepartmentView.getName())) {
                queryWrapper.like("name", performanceDepartmentView.getName());
            }
            if (StringUtils.isNotBlank(performanceDepartmentView.getSort())) {
                queryWrapper.like("sort", performanceDepartmentView.getSort());
            }
        }
            return queryWrapper;
        }
}

